﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GNBaseService.PayDemo.OrderApp.Dto.Input;
using GNBaseService.PayDemo.OrderApp.Dto.Output;
using GN.Pay.AbpClient;
using GN.Pay.Client.Request.Trade;
using AutoMapper;
using GN.Pay.Client.Response.Trade;
using GN.Pay.Client.Notify;
using GN.Pay.Client.Request.MerchantMember;

namespace GNBaseService.PayDemo.OrderApp
{
    /// <summary>
    /// 订单应用服务
    /// </summary>
    public class OrderAppService : PayDemoAppServiceBase, IOrderAppService
    {
        static OrderAppService()
        {
            Mapper.Initialize((m) =>
            {
                m.CreateMap<PayApplyResponse, OrderPayApplyOutput>();
                m.CreateMap<PayChannelResponse, OrderPayChannelApplyOutput>();
            });
        }

        private readonly IPayClientDomainService payService;

        public OrderAppService(IPayClientDomainService payService)
        {
            this.payService = payService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public TransfersApplyResponse TransfersApply(string orderNo)
        {
            var request = new TransfersApplyRequest
            {
                ApplyNo = orderNo,
                Summary = "返佣",
                NotifyUrl = "http://localhost:6699/Order/TransfersNotify",
                AccountName = "李四",
                AccountNo = "tesewewe021948484",
                ApplyMoney = 2.3m,
                ChannelId = "WeiXin.QRCode",
                ClientIp = "192.168.1.16"
            };
            var response = payService.TradeApplyService.TransfersApply(request);

            // response.ApplyState== GN.Pay.Client.TradeApplyState.Refund_Success 

            return response;
        }

        /// <summary>
        /// 订单的支付申请，通过客户端提交订单号，
        /// 获取申请号，再将申请号向支付系统网关提交，
        /// 向客户端展示支付页面
        /// </summary>
        /// <param name="input">输入</param>
        /// <returns></returns>
        public OrderPayApplyOutput OrderPayApply(OrderPayApplyInput input)
        {

            //var result = payService.MerchantMemberService.BankCardBindApply(new BankCardBindApplyRequest()
            //{
            //    BankNo = "Gyb",
            //    AccountNo = "6217359901019343540",
            //    AccountName = "杨昌国",
            //    AccountType = 1,
            //    IdentityNo = "522731198009039212",
            //    MobilePhone = "13639089699",
            //    MemberNo = "UI25487544554"
            //});

            //var result = payService.MerchantMemberService.BankCardBind(new BankCardBindRequest()
            //{
            //    SmsCheckCode = "986192",
            //    Token = "748860c4321e4a129eb24b812791ccca"

            //});

            //var banklist = payService.MerchantMemberService.BankCardList(new MemberRequest()
            //{
            //    MemberNo = "UI25487544554"
            //});

            //var result = payService.TradeApplyService.PayApplyAndChannelRegister(new PayApplyAndChannelRequest()
            //{

            //    OrderNo = input.OrderNo,
            //    Subject = "【贵农自营】订单",
            //    Body = "电视机、机顶合",
            //    ExpireMinute = 300,
            //    OrderMoney = 0.01m,
            //    OrderTime = DateTime.Now,
            //    Remark = null,
            //    MemberNo = "UI25487544554",
            //    NotifyUrl = "http://localhost:6699/Order/PayNotify",
            //    ReturnUrl = "http://localhost:6699/Order/PayReturnInfo",
            //    ExtraParam = null, //扩展参数，通知时会返回,
            //    BankCradPayId = banklist[0].Id.ToString(),
            //    ChannelId = "Gyb.ProtocolPay",
            //    ClientIP = "152.251.456.25",
            //});

            //var result = payService.TradeApplyService.SmsPay(new SmsPayRequest()
            //{
            //    PayId = "fc156bb2f04f4cddb6506eb4d3090efc",
            //    SmsCheckCode = "218166"

            //});



            //if (result != null)
            //{

            //}



            //检查订单业务状态
            //获取订单信息
            //其他处理，也可以领域服务处理，PayAbpClient本身就是一个领域服务            
            PayApplyRequest request = new PayApplyRequest
            {
                OrderNo = input.OrderNo,
                Subject = "【贵农自营】订单",
                Body = "测试商品长度",
                ExpireMinute = 60*24*3,
                OrderMoney = 0.01m,
                OrderTime = DateTime.Now,
                Remark = null,
                MemberNo = "IUO652445586516",
                UserToken = "od3kFwryFKuNcFvY1907wNDrhKXs",
                NotifyUrl = "http://localhost:6699/Order/PayNotify",
                ReturnUrl = "http://localhost:6699/Order/PayReturnInfo",
                ExtraParam = null //扩展参数，通知时会返回,

            };
            try
            {
                //var dss = payService.TradeApplyService.PayApplyAndChannelRegister(new PayApplyAndChannelRequest()
                //{
                //    Body = request.Body,
                //    ChannelId = "AliPay.MoblieWap",
                //    ClientIP = "111.121.74.141",
                //    ExpireMinute = 30,
                //    OrderMoney = 0.01M,
                //    OrderNo = request.OrderNo,
                //    OrderTime = request.OrderTime,
                //    Subject = request.Subject,
                //    ExtraParam = request.ExtraParam,
                //    Remark = request.Remark
                //});

                //if (dss != null)
                //{

                //}

                //var r1 = payService.TradeApplyService.PayApplyAndChannelRegister(new PayApplyAndChannelRequest()
                //{

                //    OrderNo = input.OrderNo,
                //    Subject = "【贵农自营】订单",
                //    Body = "电视机、机顶合",
                //    ExpireMinute = 300,
                //    OrderMoney = 0.01m,
                //    OrderTime = DateTime.Now,
                //    ChannelId = "AliPay.MoblieWap",
                //    ClientIP = "111.121.76.140",
                //    Remark = null,
                //    MemberNo = "FTT25487544554",
                //    ShowUrl = "http://www.guinong.com/m-wap",
                //    NotifyUrl = "http://localhost:6699/Order/PayNotify",
                //    ReturnUrl = "http://localhost:6699/Order/PayReturnInfo",
                //    ExtraParam = null //扩展参数，通知时会返回,
                //});

                //if (r1 != null)
                //{

                //}

                var response = payService.TradeApplyService.PayApplyRegister(request);
                return Mapper.Map<PayApplyResponse, OrderPayApplyOutput>(response);
            }
            catch (Exception err)
            {
                if (err != null)
                {

                }
                throw err;
            }

        }

        /// <summary>
        /// 订单支付通道申请，指由客户端已明确指定哪种支付方式进行支付，
        /// 特别是原生支付，或自行实现支付界面则调用此函数，该函数将直接返回
        /// 第三方的已签名支付脚本或支付参数，直接向第三方控件(app)或调用脚本支付。
        /// </summary>
        /// <param name="input">输入</param>
        /// <returns></returns>
        public OrderPayChannelApplyOutput OrderPayChannelApply(OrderPayChannelApplyInput input)
        {
            //检查订单业务状态
            //获取订单信息
            //其他处理，也可以领域服务处理，PayAbpClient本身就是一个领域服务
            PayApplyAndChannelRequest request = new PayApplyAndChannelRequest
            {
                OrderNo = input.OrderNo,
                Subject = "【贵农自营】订单",
                Body = "电视机、机顶合",
                ExpireMinute = 30,
                OrderMoney = new Random().Next(1, 10) / 100m,
                OrderTime = DateTime.Now,
                Remark = "送电视架",
                ChannelId = input.ChannelId,
                ClientIP = input.ClientIP,
                NotifyUrl = "http://localhost:6699/Order/PayNotify",
                ReturnUrl = "http://localhost:6699/Order/PayReturnInfo",
                ExtraParam = "" //扩展参数，通知时会返回,
            };
            var response = payService.TradeApplyService.PayApplyAndChannelRegister(request);
            return Mapper.Map<PayChannelResponse, OrderPayChannelApplyOutput>(response);
        }

        /// <summary>
        /// 付款通知，不可对外开放WebApi
        /// </summary>
        /// <param name="message"></param>
        public void PayNotify(PayNotifyMessage message)
        {
            //收款处理逻辑,出现异常表示处理失败，否则视为成功
            //通知即使返回成功，也有可能再次通知，因此若相同的交易号和订单号已处理，则不在处理，但不要返回异常
        }

        /// <summary>
        /// 退款通知，不可对外开放WebApi
        /// </summary>
        /// <param name="message"></param>
        public void RefundNotify(RefundNotifyMessage message)
        {
            //退款处理逻辑,出现异常表示处理失败，否则视为成功
            //通知即使返回成功，也有可能再次通知，因此若相同的申请号和订单号已处理，则不在处理，但不要返回异常
        }

        //其他说明
        //1、退款Api不应当对外开放，而是走过内部流程，直接调用
        //2、其他Api使用类似。也可不用定义输入输出，直接使用ClientApi自带，但付款申请应当由内部业务系统生成相关参数
    }
}
